# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=onnx
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=1.17.0
pkgrel=2
pkgdesc="Open standard for machine learning interoperability. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://www.onnx.ai'
msys2_repository_url="https://github.com/onnx/onnx"
msys2_references=(
  'purl: pkg:pypi/onnx'
)
license=('spdx:Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-protobuf"
             "${MINGW_PACKAGE_PREFIX}-pybind11"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-numpy"
             "${MINGW_PACKAGE_PREFIX}-python-protobuf")
source=("https://github.com/onnx/onnx/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-fix-build-on-mingw-w64.patch")
sha256sums=('8d5e983c36037003615e5a02d36b18fc286541bf52de1a78f6cf9f32005a820e'
            '33ae2b4039973e3b736757cd5fa3e683dc76a0b1e76d45817a1b7d1d8e390e62')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-fix-build-on-mingw-w64.patch
}

build() {
  rm -rf "python-build-${MSYSTEM}"
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"

  declare -a _cmake_args
  if check_option "debug" "n"; then
    _cmake_args+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _cmake_args+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  _cmake_args+=(
    "-Wno-dev"
    "-GNinja"
    "-DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX}"
    "-DBUILD_SHARED_LIBS=ON"
    "-DONNX_USE_PROTOBUF_SHARED_LIBS=ON"
  )

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ONNX_BUILD_TESTS=0 \
  CMAKE_ARGS="${_cmake_args[@]}" \
  python -m build --wheel --skip-dependency-check --no-isolation
}

package_onnx() {
  depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
           "${MINGW_PACKAGE_PREFIX}-protobuf")

  DESTDIR="${pkgdir}" cmake --install python-build-${MSYSTEM}/.setuptools-cmake-build

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

package_python-onnx() {
  depends=("${MINGW_PACKAGE_PREFIX}-python"
           "${MINGW_PACKAGE_PREFIX}-python-protobuf"
           "${MINGW_PACKAGE_PREFIX}-python-numpy"
           "${MINGW_PACKAGE_PREFIX}-onnx"
           "${MINGW_PACKAGE_PREFIX}-gcc-libs"
           "${MINGW_PACKAGE_PREFIX}-protobuf")
  options=(!strip)

  cd "${srcdir}/python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
